﻿(function ($) {

    $.drawPaging = {

        init: function (numOfItems, itemsInOnePage) {
            var numOfPages = numOfItems / itemsInOnePage;
            var remainder = numOfItems % itemsInOnePage;
            if (remainder > 0) {
                numOfPages = numOfPages + 1;
            }
            return numOfPages;
        },
        hide: function () {
            var $mainDiv = this.find('div.paging');
            this.$mainDiv.hide();
        },
        stop: function (index) {
            var $li_Tot = $("#Paging_UL li").length;
            if (index != $li_Tot && index != -1) {
                return true;
            }
            return false;
        }
    };

    var settings = {
        paging: true,
        type: '',
        numOfItems: '1000',
        itemsInOnePage: '200',
        maxPagesToShow: '5',
        motion: ''
    };

    $.fn.Paging = function (options) {
        // Method calling logic
        var $maxPagesToShow = parseInt(options.maxPagesToShow);
        var $motion = options.motion;
        $.extend(this, {

            clickHandler: function (index) {

            },

            next: function (index) {
                if ($.drawPaging.stop(index)) {
                    if (index >= $maxPagesToShow) {
                        this.show_next_selected();
                    }
                    this.next_notselected()
                    this.enable_controls();
                }
                else {
                    this.disable_next();
                }
            },

            prev: function (index) {
                if ($.drawPaging.stop(index)) {
                    this.show_prev_selected();
                    this.prev_notselected();
                    this.enable_controls();
                }
                else {
                    this.disable_prev();
                }
            },

            show_next_selected: function () {
                var current = $("#Paging_UL li.selected");
                var $new_p_No = (current.index() + 1) - $maxPagesToShow;
                $("#Paging_UL").find("li:eq(" + $new_p_No + ")")
                .css("display", "none");
                current.next().css("display", "");
            },

            show_prev_selected: function () {
                var current = $("#Paging_UL li.selected");
                var $new_p_No = (current.index() - 1) + $maxPagesToShow;
                $("#Paging_UL").find("li:eq(" + $new_p_No + ")")
               .css("display", "none");
                current.prev().css("display", "");
            },

            next_notselected: function () {
                var current = $("#Paging_UL li.selected");
                current.removeClass("selected")
                current.next().addClass("selected");
            },

            prev_notselected: function () {
                var current = $("#Paging_UL li.selected");
                current.removeClass("selected")
                current.prev().addClass("selected");
            },

            disable_next: function () {
                $(".next").removeClass("next")
                .addClass("no_more_pages");
            },

            disable_prev: function () {
                $(".prev").removeClass("prev")
                .addClass("no_more_pages");
            },

            enable_controls: function () {
                if ($(".next").attr("class") != "next") {
                    $(".no_more_pages").removeClass("no_more_pages")
                .addClass("next");
                }

                if ($(".prev").attr("class") != "prev") {
                    $(".no_more_pages").removeClass("no_more_pages")
                .addClass("prev");
                }

            }
        });

        $.extend(settings, options);

        var p_main_const = this;


        if (options.paging) {
            var numOfPages = $.drawPaging.init(options.numOfItems, options.itemsInOnePage);
            var $outerDiv = this.find('div.paging');
            if ($outerDiv.is(":visible") == false) {
                this.show();
            }
            $("#Paging_UL li").remove();
            $outerDiv = this.append('<div style="padding-top: 8px;"><div class="no_more_pages">Previous</div></div>');
            $outerDiv = this.append('<div class="main_Game"><ul id="Paging_UL"></ul></div>');

            for (var i = 1; i <= numOfPages; i++) {
                if (i == 1) {
                    $("#Paging_UL").append('<li id="d' + i + '" class="selected">' + i + '</li>');
                }
                else if (i <= $maxPagesToShow) {
                    $("#Paging_UL").append('<li id="d' + i + '">' + i + '</li>');
                }
                else if (i > $maxPagesToShow) {
                    $("#Paging_UL").append('<li id="d' + i + '" style="display:none">' + i + '</li>');
                }
            }
            $outerDiv = this.append('<div style="padding-top: 8px;"><div class="next">Next</div></div>');
        }
        else {
            $.drawPaging.hide($outerDiv);
        }

        $(".next").click(function () {
            var $index = $("li.selected").index();
            p_main_const.next($index + 1);
        });

        $(".paging div").first().click(function () {
            var $index = $("li.selected").index();
            p_main_const.prev($index - 1);
        });

        $("#Paging_UL li").click(function () {
            var $ul = $("#Paging_UL li");
            var $index = $(this).index();
            $(this).addClass("selected");
            if ($index == 0) {
                p_main_const.disable_prev();
            }
            else if ($index > 0 && $index < $ul.last().index()) {
                p_main_const.enable_controls();
            }
            else if ($index == $ul.last().index()) {
                p_main_const.disable_next();
            }
            else {
                p_main_const.enable_controls();
            }
            var current = $("#Paging_UL li.selected");
            current.removeClass("selected")
            $(this).addClass("selected");
        });

        $(this).attr('unselectable', 'on')
           .css('-moz-user-select', 'none')
           .each(function () {
               this.onselectstart = function () { return false; };
           });
    };
})(jQuery);
